package com.cysoft.service;

import com.cysoft.dao.MoldDao;
import com.cysoft.domain.Mold;
import com.cysoft.domain.MoldWorkState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;

@Component
public class MoldService {
    @Autowired
    private MoldDao moldDao;

    /**
     * 模具列表，用于任务分配
     */
    private List<Mold> moldList;
    private Map<Long,MoldWorkState> moldWorkStateMap;

    public List<Mold> getMoldList(){
        // 只初始化一次
        if(moldList == null || moldList.isEmpty()) {
            moldList = new ArrayList<>();
            moldList.addAll(moldDao.getMoldListFromDB());
            moldWorkStateMap = new HashMap<>(moldList.size()*2);
            for (Mold mold:moldList) {
                moldWorkStateMap.put(mold.getId(),new MoldWorkState(mold.getId(),mold));
            }
        }

        return moldDao.getMoldListFromDB();
    }

    List<Mold> queryMoldList(String wheelCode) {
        if(moldList == null || moldList.isEmpty()) {
            getMoldList();
        }
        List<Mold> newMoldList = new ArrayList<>();
        List<Mold> moldList1 = new ArrayList<>();
        List<Mold> moldList0 = new ArrayList<>();
        for (Mold mold: moldList ) {
            if(mold.getType()==1 && mold.getWheelCode().equals(wheelCode) ) {
                moldList1.add(mold);
            }
            if(mold.getType()==0 && mold.getWheelCode().equals(wheelCode) ) {
                moldList0.add(mold);
            }
        }

        // 避免重复
        Set<Mold> matchMoldList = new HashSet<>();
        for (Mold mold0 : moldList0) {
            for (Mold mold1 : moldList1) {
                if(mold1.getName().equals(mold0.getName())) {
                    matchMoldList.add(mold1);
                }
            }
        }
        matchMoldList.addAll(moldList0);

        newMoldList.addAll(matchMoldList);
        newMoldList.sort(new Comparator<Mold>() {
            @Override
            public int compare(Mold o1, Mold o2) {
                if(o1.getType() == o2.getType() && o1.getName().compareTo(o2.getName())==0) {
                    return 0;
                }
                if(o1.getType() >  o2.getType()) {
                    return -1;
                } else if(o1.getType() < o2.getType()){
                    return 1;
                }
                int comp = o1.getName().compareTo(o2.getName());
                if(comp > 0) {
                    return -1;
                }else if(comp < 0) {
                    return 1;
                }
                return comp;
            }
        });
        return newMoldList;
    }
    /**
     * 分配模具
     * 优先级：优先取是可用模具且是减重的模具
     * 减重模具M5-M3，可用模具M4-M3，则取M4，M4用完用M3
     * @param wheelCode
     * @param beginDateTime
     * @return
     */
    public Mold assignMold(String wheelCode, LocalDateTime beginDateTime,LocalDateTime endDateTime) {
        // 根据名称降序排列，姑且认为是按照优先级排序
//        List<Mold> molds = moldDao.getMoldListByWheelCode(wheelCode);
        List<Mold> molds = queryMoldList(wheelCode);
        MoldWorkState moldWorkState;
        for (Mold mold:molds) {
            moldWorkState = moldWorkStateMap.get(mold.getId());
            // 如果模具可用
            if(moldWorkState.checkValid(beginDateTime,endDateTime)) {
                updateMoldUseState(moldWorkState,beginDateTime,endDateTime);
                return moldWorkState.getMold();
            }
        }

        return null;
    }

    public void resetMoldState(long oldMoldId) {
        MoldWorkState moldWorkState = moldWorkStateMap.get(oldMoldId);
        if(moldWorkState != null) {
            moldWorkState.resetState();
        }
    }
    public Mold assignMold(long taskId,String wheelCode, LocalDateTime beginDateTime,LocalDateTime endDateTime) {
        // 根据名称降序排列，姑且认为是按照优先级排序
//        List<Mold> molds = moldDao.getMoldListByWheelCode(wheelCode);
        List<Mold> molds = queryMoldList(wheelCode);
        MoldWorkState moldWorkState;

        for (Mold mold:molds) {
//            if(oldMoldWorkState != null && oldMoldId != mold.getId()) {
//                oldMoldWorkState.resetState();
//            }

            moldWorkState = moldWorkStateMap.get(mold.getId());
            // 如果模具可用
            if(taskId == mold.getTaskId() || moldWorkState.checkValid(beginDateTime,endDateTime)) {
                updateMoldUseState(moldWorkState,beginDateTime,endDateTime);
                return moldWorkState.getMold();
            }
        }

        return null;
    }
    public boolean updateMoldUseState(MoldWorkState moldWorkState,LocalDateTime beginDateTime,LocalDateTime endDateTime) {
        if(moldWorkState != null) {
            moldWorkState.setBeginWorkTime(beginDateTime);
            moldWorkState.setEndWorkTime(endDateTime);
            return  true;
        }
        return  false;
    }

    public List<Mold> getMoldListByWheelCode(String code){

        return moldDao.getMoldListByWheelCode(code);
    }


}
